Skip to content

trystero: hold a second connection until the peer shows which it keeps (fixes interop room-leave flake) - #9

Merged
pinhopro merged 1 commit into
mainfrom
fix/interop-room-leave
Nov 20, 2026
Merged

pinhopro merged 1 commit into
mainfrom
fix/interop-room-leave

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

Fixes the intermittent failure of interop_native's "Native C++: one room leaving, either side, leaves the other room and the connection" on webcpp main (CI run 44726002584, js#1: console.error Trystero peer error: OperationError).

Cause

The failure happens before any room leaves, while the scenario's two rooms negotiate at once. Sometimes the two connections cross: each side keeps the connection whose channel opened first on its side (Trystero's rule, dist/shared-peer.mjs:105-108), and the two sides pick different ones. The native app then closed its second connection at once. That was the connection the page kept, and the page was just starting to send on it.

libdatachannel ends an association with a stream reset and a SHUTDOWN, and its ABORT comes a moment later. When a browser closes, its ABORT follows the reset at once. With the native close, Chrome's channel finished the closing procedure, or saw the SHUTDOWN, while one of the page's sends was in flight. It failed that send and closed with data-channel-failure. Trystero JS ignores only an abort (dist/peer.mjs:157-160), so it logged a peer error.

Evidence:

  • Chrome's log shows OnIncomingStreamsReset, then Send failed INVALID_STATE, then OnStreamsResetPerformed, then DataChannel error: "", and only after that the native ABORT.
  • An instrumented loop of 1,800 two-room meetings had 46 crossed pairs, and 6 of them made the page report the error.

Making the native close W3C-like does not help. I tried closing the connection before its channel, so that no stream reset is sent. That made things worse: Chrome then reported Failure to send data from the SHUTDOWN in 6 of 20 crossed pairs. libdatachannel has no way to close with an ABORT alone.

Fix

The app no longer closes a second connection to a peer at once. It holds it, unused, until the peer shows which connection it keeps:

  • The peer sends a frame on the first: the second is closed. A connection that opens after the peer has sent on the first is closed at once.
  • The peer closes the first (Trystero JS does this in a crossed pair): the second replaces the first. It takes the first's place before the rooms hear of the end, then replays the frames it received meanwhile.
  • The peer sends on the second, and this side has the higher id: the second replaces the first. This lets two peers of this port settle, since both hold their second.
  • The peer leaves it alone: the higher id closes it after 2 s, the lower id after 10 s.

Results

  • Crossed pairs no longer make the page report a peer error. In 1,800 meetings: 0 page errors and no slow setups.
  • The scenario passed 1,200 runs of 1,200 locally (6 parallel loops), against about 1 failure in 300 before.
  • The full interop_native suite passes, 30 of 30.
  • The emscripten interop_browser suite passes, 26 of 26, and so do app_impl and shared on emscripten.
  • All native trystero tests pass.

Tests and docs

  • New app_impl tests cover each outcome: the second closed, closed at once, replaced by the lower id, taken by the higher id, and the deadlines.
  • New connection tests cover used, ended_by_transport and replay.
  • The memory relays now hold a frame that reaches an end before it opens, as a real channel does. The crossed-pair shared test now checks that the lower id never loses the peer.
  • differences.adoc now describes the change, with the reason, and the changelog line is updated.

The superproject PR bumps the gitlink and updates CODING-GUIDELINES.md's adoption decision.

🤖 Generated with Claude Code

…which it keeps, so that a crossed pair no longer makes a Trystero JS page report a peer error

The interop lane's "one room leaving" scenario failed on main (run 44726002584) with
"console.error Trystero peer error: OperationError" before any room left: its two rooms
negotiated at once, the two channels opened in one order on the page and in the other natively,
and each side kept the one that opened first on its side, as Trystero's rule says. The native app
then closed its second connection at once, which was the one the page kept, just as the page sent
its first frames on it. libdatachannel ends an association with a stream reset and a SHUTDOWN,
its ABORT a moment later, where a browser's ABORT follows its reset at once: Chrome's channel
completed the closing procedure (or saw the SHUTDOWN) with a send failed in between, closed with
that failure ("data-channel-failure", or "Failure to send data"), and Trystero JS, which ignores
only an abort, logged a peer error. Chrome's log showed the reset, "Send failed INVALID_STATE",
then the error, ahead of the ABORT; in 1,800 two-room meetings the page reported 6 such errors
among 46 crossed pairs.

The app now holds such a second connection, unused, until the peer shows which it keeps: a frame
on the first, which closes the second as the peer closes its own (and a connection opened after
the peer sent on the first is closed at once); the peer closing the first, which Trystero JS does
in a crossed pair, after which the second replaces the first with the frames it received; or,
for the higher id, a frame on the second, which then replaces the first, since a peer of this
port holds its second too. One the peer leaves alone is closed after 2 s by the higher id and
after 10 s by the lower id. No connection the peer uses is closed, so the 1,800 meetings showed
no error, and the scenario passed 600 runs of 600.

The memory relays now hold a frame that reaches an end before it opens until it opens, as a real
channel delivers what follows its opening message; the crossed-pair test now checks the lower
id keeps the peer throughout.
@pinhopro
pinhopro merged commit 0f7e6f4 into main Nov 20, 2026
16 of 30 checks passed
@pinhopro
pinhopro deleted the fix/interop-room-leave branch November 20, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant